<template>
  <view class="query_container">
    <!-- 开启霸王餐的活动 -->
    <view class="bargin_container"> </view>
    <!-- 开启霸王餐的活动 -->
    <!-- 商品展示区域 -->
    <view class="list" v-if="!rule_show">
      <scroll-view
        :scroll-y="true"
        class="scroll-y"
        @scrolltolower="scroll_bottom"
      >
        <bargin-good
          :goodsList="goodList"
          v-if="goodList.length > 0"
          @audi="audi"
          @edit="edit"
        ></bargin-good>
      </scroll-view>
      <!-- 商品展示区域 -->
      <view class="empty" style="text-align: center" v-if="goodList.length == 0"
        >暂无商品</view
      >
    </view>

    <view class="add_rule_container" v-if="rule_show">
      <view
        class="start_pick_container"
        @tap="choose_time"
        style="margin-bottom: 10rpx"
      >
        <van-field
          :value="value"
          :border="false"
          label="砍价倒计时时间"
          disabled
        />
        <view v-if="!countdown" class="placeholder">请选择砍价倒计时时间</view>
        <view v-if="bargin_ruleForm.hour" class="placeholder">{{
          countdown
        }}</view>
      </view>
      <van-field
        :value="bargin_ruleForm.need_times"
        placeholder="请输入次数"
        :border="false"
        @change="change_need_times"
        label="需要砍多少次"
      />
      <van-field
        :value="bargin_ruleForm.one_people_times"
        placeholder="请输入每人次数"
        :border="false"
        @change="change_per_times"
        label="每人次数(默认次数)"
      />
      <van-field
        :value="bargin_ruleForm.cut_item_title"
        placeholder="请输入自定义名称"
        :border="false"
        @change="change_cut_title"
        type="textarea"
        autosize
        label="自定义名称"
      />
      <van-field
        :value="bargin_ruleForm.price"
        placeholder="请输入商品价格"
        :border="false"
        disabled
        label="商品价格"
      />
      <van-field
        :value="bargin_ruleForm.cut_price"
        placeholder="请输入保底价"
        :border="false"
        @change="change_cut_price"
        label="保底价"
      />
      <van-field
        :value="bargin_ruleForm.success_times"
        placeholder="请输入已成功次数"
        :border="false"
        @change="change_suc_times"
        label="成功次数"
      />
      <van-field
        :value="bargin_ruleForm.open_times"
        placeholder="请输入活动被开启次数"
        :border="false"
        @change="change_open_times"
        label="已开启次数"
      />
      <van-cell>
        <view slot="title">{{
          bargin_ruleForm.show_need_times == 1
            ? "显示砍价所需刀数"
            : "不显示所需刀数"
        }}</view>
        <view slot="">
          <van-switch
            :checked="bargin_ruleForm.show_need_times == 1 ? true : false"
            @change="change_show_need_times"
          />
        </view>
      </van-cell>
      <van-cell>
        <view slot="title">{{
          bargin_ruleForm.show_bj_price == 1 ? "显示保底价" : "不显示保底价"
        }}</view>
        <view slot="">
          <van-switch
            :checked="bargin_ruleForm.show_bj_price == 1 ? true : false"
            @change="change_show_bj_price"
          />
        </view>
      </van-cell>
      <view style="padding-left: 30upx">图片(图片大小不超过1m)</view>
      <view class="upload" style="padding-left: 20upx">
        <uImg
          ref="upimg"
          :canUploadFile="true"
          :limit="limit"
          :uploadFileUrl="uploadFileUrl"
          :uImgList.sync="uImgList"
          @uploadSuccess="uploadSuccess"
          :fileKeyName="name"
        />
        <!-- <van-button
          type="primary"
          size="mini"
          style="margin-left: 20upx"
          @tap="upload"
          >确定上传</van-button
        > -->
      </view>
      <div class="btn_box">
        <van-button @tap="submit" size="large" color="#f95a4b">提交</van-button>
        <van-button @tap="cancle_submit" size="large">返回</van-button>
      </div>
    </view>
    <w-picker
      :visible="show_time_picker"
      mode="time"
      :value="bargin_ruleForm.hour"
      :second="false"
      @confirm="confirm_time1"
      @cancel="cancle_time1"
      ref="date1"
      :current="false"
    ></w-picker>
    <van-dialog id="van-dialog" />
    <van-toast id="van-toast" />
  </view>
</template>

<script>
import BarginGood from "../near_mch_bargin/components/can_bargin_goods";
import uImg from "../../components/zhtx-uploadImg/zhtx-uploadImg";
import { cutprice_list1, item_save,cutprice_approve } from "../../utils/imapi";
import Dialog from "../../wxcomponents/vant/dialog/dialog";
export default {
  props: {},
  components: {
    BarginGood,
    uImg,
  },
  data() {
    return {
      value: "",
      option1: [
        { text: "全部", value: 0 },
        { text: "待付款", value: 1 },
        { text: "待发货", value: 2 },
        { text: "交易完成", value: 3 },
        { text: "售后", value: 4 },
        { text: "交易取消", value: 5 },
      ],
      goodList: [],
      value1: 0,
      queryparam: {
        page_index: 1,
        page_size: 10,
        status: "",
        create_time_start: "",
        create_time_end: "",
        cut_mode: "3",
        approve_status: "0",
      },
      bargin_ruleForm: {},
      rule_show: false,
      uImgList: [],
      value: "",
      uploadFileUrl:
        "https://api.comm.t.xingkonglian.net:8098/api/v1/oss/upload_starx",
      limit: 1,
      name: "file",
      show_time_picker: false,
      minHour: "00",
      maxHour: "23",
      countdown: "",
    };
  },
  computed: {},
  watch: {},
  onLoad() {
    this.cutprice_list();
  },
  onReady() {},
  onShow() {},
  methods: {
    async cutprice_list() {
      try {
        const data = await cutprice_list1(this.queryparam);
        console.log(data);
        if (data.code == 0) {
          this.goodList = data.data;
          console.log(this.goodList);
        }
      } catch (error) {
        console.log(error);
      }
    },
    status_change(e) {
      console.log(e.detail);
    },
    inputchange(e) {
      console.log(e.detail);
      this.queryparam.title = e.detail.value;
    },
    reset() {
      this.queryparam = {
        goods_status: "",
        page_index: 1,
        page_size: 10,
        title: "",
      };
      this.query_all_goods();
    },
    thequery() {
      console.log(this.queryparam.title);
      this.query_all_goods();
    },
    async scroll_bottom() {
      console.log("触底了");
      this.queryparam.page_index++;
      try {
        const data = await query_all_goods(this.queryparam);
        this.$toast.loading("加载中");
        if (data.code == 0) {
          this.goodList.push(...data.data);
          console.log(data);
          if (data.data.length == 0) {
            this.queryparam.page_index--;
            this.$toast("全部加载");
          }
        }
      } catch (error) {}
    },
    go_add_goods() {
      uni.navigateTo({
        url: "/pages/mch_goods/mch_goods?from=near",
      });
    },
    audi(e) {
      console.log(e);
      Dialog.confirm({
        title: "审核此砍价模板",
        confirmButtonText:'通过',
        cancelButtonText:'未通过'
      })
        .then(async () => {
          // on confirm
          const data = await cutprice_approve({
            id: e.id,
            approve_status : "1",
            remark:''
          });
          if (data.code == 0) {
            this.$toast.success("操作成功");
            this.cutprice_list();
          }
        })
        .catch(() => {
          // on cancel
        });
    },
    edit(e) {
      this.bargin_ruleForm = e;
      this.countdown = e.hour + ":" + e.minutes;
      this.bargin_ruleForm.hour=e.hour.toString()
      this.bargin_ruleForm.minutes=e.minutes
      console.log(this.countdown);
      this.uImgList = [];
      this.uImgList.push(e.pos_pic);
      this.rule_show = true;
    },
    change_need_times(e) {
      this.bargin_ruleForm.need_times = e.detail;
    },
    change_per_times(e) {
      this.bargin_ruleForm.one_people_times = e.detail;
    },
    change_cut_title(e) {
      this.bargin_ruleForm.cut_item_title = e.detail;
    },
    change_cut_price(e) {
      this.bargin_ruleForm.cut_price = e.detail;
    },
    change_suc_times(e) {
      this.bargin_ruleForm.success_times = e.detail;
    },
    change_open_times(e) {
      this.bargin_ruleForm.open_times = e.detail;
    },
    change_show_need_times(e) {
      console.log(e);
      this.bargin_ruleForm.show_need_times = e.detail ? 1 : 0;
    },
    change_show_bj_price(e) {
      console.log(e);
      this.bargin_ruleForm.show_bj_price = e.detail ? 1 : 0;
    },
     choose_time() {
      this.show_time_picker = true;
    },
    uploadSuccess(result) {
      if (result.statusCode == 200) {
        //上传成功的回调处理
        console.log(result.data);
        const img = JSON.parse(result.data).data;
        console.log(img);
        this.bargin_ruleForm.pos_pic = img;
        console.log(result);
      } else {
      }
    },
    async submit() {
      console.log(this.bargin_ruleForm);
      if (
        this.bargin_ruleForm.need_times == "" ||
        this.bargin_ruleForm.one_people_times == "" ||
        this.bargin_ruleForm.cut_item_title == "" ||
        this.bargin_ruleForm.pos_pic == "" ||
        this.bargin_ruleForm.price == "" ||
        (this.bargin_ruleForm.open_times == "" &&
          this.bargin_ruleForm.open_times != 0) ||
        (this.bargin_ruleForm.success_times == "" &&
          this.bargin_ruleForm.success_times != 0) ||
        (this.bargin_ruleForm.cut_price == ""&&this.bargin_ruleForm.cut_price!=0)
      ) {
        this.$toast("请填写完整数据");
        return;
      }
      this.bargin_ruleForm.cut_price = Number(this.bargin_ruleForm.cut_price);
      this.bargin_ruleForm.hour = Number(this.bargin_ruleForm.hour);
      this.bargin_ruleForm.minutes = Number(this.bargin_ruleForm.minutes);
      this.bargin_ruleForm.need_times = Number(this.bargin_ruleForm.need_times);
      this.bargin_ruleForm.one_people_times = Number(
        this.bargin_ruleForm.one_people_times
      );
      this.bargin_ruleForm.open_times = Number(this.bargin_ruleForm.open_times);
      this.bargin_ruleForm.price = Number(this.bargin_ruleForm.price);
      this.bargin_ruleForm.success_times = Number(
        this.bargin_ruleForm.success_times
      );
      this.bargin_ruleForm.show_need_times = Number(
        this.bargin_ruleForm.show_need_times
      );
      this.bargin_ruleForm.show_bj_price = Number(
        this.bargin_ruleForm.show_bj_price
      );
      this.bargin_ruleForm.limit_radius_pub = Number(
        this.bargin_ruleForm.limit_radius_pub
      );
      try {
        const data = await item_save(this.bargin_ruleForm);
        if (data.code == 0) {
          this.$toast.success("编辑成功");
          this.rule_show = false;
          this.cutprice_list();
        }
      } catch (err) {
        console.log(err);
        this.$toast.success("编辑失败");
      }
    },
    confirm_time1(e) {
      console.log(e);
      this.countdown = e.result;
      this.bargin_ruleForm.hour = e.result.split(":")[0];
      this.bargin_ruleForm.minutes = e.result.split(":")[1];
      console.log(this.bargin_ruleForm);
      this.show_time_picker = false;
    },
    cancle_time1() {
      this.show_time_picker = false;
    },
    cancle_submit() {
      console.log("111");
      this.rule_show = false;
    },
    upload() {
      this.$refs.upimg.upload();
    },
  },
  onReachBottom() {},
  onHide() {},
};
</script>

<style scoped lang="scss" scoped>
.query_container {
}
.btn_box {
  padding: 0 20px;
  margin-top: 20upx;
  /deep/ .van-button:first-child {
    margin-bottom: 20upx;
  }
}
.upload {
  display: flex;
  align-items: center;
}
.start_pick_container {
  display: flex;
  align-items: center;
  position: relative;
  .placeholder {
    position: absolute;
    top: 50%;
    left: 130px;
    transform: translateY(-50%);
    color: #333333;
  }
}
/deep/ .van-cell__title {
  width: 250upx;
  max-width: 250upx !important;
}
</style>
